package assignment;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2025-02-26
 * Time: 12:06
 */
public class Demo9 {
    public static void main(String[] args) {
        // 创建线程池
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                5, // 核心线程数
                10, // 最大线程数
                60L, // 空闲线程存活时间
                TimeUnit.SECONDS, // 时间单位
                new ArrayBlockingQueue<>(100), // 任务队列，容量为100
                new ThreadPoolExecutor.AbortPolicy() // 拒绝策略，忽略最新任务
        );

        // 测试线程池
        for (int i = 0; i < 200; i++) {
            int taskNumber = i;
            threadPool.execute(() -> {
                System.out.println("执行任务: " + taskNumber + "，线程: " + Thread.currentThread().getName());
                try {
                    Thread.sleep(1000); // 模拟任务执行时间
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        // 关闭线程池
        threadPool.shutdown();
    }
}
